package com.hk.servlet.company;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.hk.model.ContactUs;
import com.hk.model.vo.Result;
import com.hk.model.vo.ResultVO;
import com.hk.service.IContactUsService;
import com.hk.service.impl.ContactUsServiceImpl;
import com.hk.utils.CharacterEncoding;
import com.hk.utils.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author: zhe
 * @Date: 2021/6/15 11:29
 * @Statement: 联系我们，查询所有和添加信息
 */
@WebServlet("/contactUs/ContactUsQueryOrSave")
public class ContactUsQueryOrSaveServlet extends HttpServlet {

    IContactUsService iContactUsService = new ContactUsServiceImpl();

    /**
     * 1、分页查询所有联系我们信息
     * 2、根据公司地址模糊查询
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CharacterEncoding.setCharacterEncodingResp(req,resp,"UTF-8");
        String page = Tool.nullToString(req.getParameter("page"));
        String limit = Tool.nullToString(req.getParameter("limit"));
        String site = Tool.nullToString(req.getParameter("site"));
        int i = Tool.parseInt(page);
        int size = Tool.parseInt(limit);
        //        根据sql语句规则将每页页码修改为需要数值
        int pageNum = Tool.pageNum(i,size);
//        封装对象
        ContactUs contactUs = new ContactUs();
        contactUs.setSite(site);
//        查询数据
        List<ContactUs> contactUsList = iContactUsService.findAll(pageNum, size, contactUs);
//        查询总数
        int count = iContactUsService.count(-1,-1, contactUs);
        PrintWriter out = resp.getWriter();
        if (contactUsList.isEmpty()) {
            ResultVO<ContactUs> resultVO = new ResultVO<>(0,"error",0,contactUsList);
            String contactUsListJSON = JSONObject.toJSONString(resultVO);
            out.println(contactUsListJSON);
        } else {
            ResultVO<ContactUs> resultVO = new ResultVO<>(0,"success",count,contactUsList);
            String contactUsListJSON = JSONObject.toJSONString(resultVO);
            out.println(contactUsListJSON);
        }
        out.flush();
        out.close();

    }

    /**
     * 新增联系我们信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CharacterEncoding.setCharacterEncodingResp(req,resp,"UTF-8");
        BufferedReader reader = req.getReader();
        Gson gson= new Gson();
        ContactUs contactUs = gson.fromJson(reader, ContactUs.class);
        int save = iContactUsService.save(contactUs);
        PrintWriter out = resp.getWriter();
        Result result = new Result();
        if (save != 1) {
            result.setStatus(save);
            result.setMessage("error");
        } else {
            result.setStatus(save);
            result.setMessage("success");
        }
        String resultJSON = JSONObject.toJSONString(result);
        out.println(resultJSON);
        out.flush();
        out.close();

    }

}
